import type { AxiosResponse } from 'axios';
import { message } from 'antd';
import { StatusEnum } from './config';

/**
 * @description: 校验网络请求状态码
 * @param {AxiosResponse} response
 * @return void
 */
export const checkStatus = (response: AxiosResponse): void => {
  let text: string = '';
  const { data, status } = response;
  switch (status) {
    case 400:
      text = '请求失败！请您稍后重试';
      break;
    case 401:
      text = '身份已过期，请重新登录！';
      break;
    case 404:
      text = '请求失败！请您稍后重试';
      break;
    case 500:
      text = '服务器出错了！请您稍后重试';
      break;
    default:
      text = response.statusText || '请求失败！';
  }

  if (status === StatusEnum.NO_AUTH) {
    // 登录信息失效，应跳转到登录页面，并清空本地的token
    // localStorage.setItem('token', '')
    // router.replace({
    //   path: '/login'
    // })
  } else {
    message.error(data.message || text);
  }
};
